#include "stdafx.h"

using namespace rengine3d;

class CMainApp: public IUpdateable {
public:
	CMainApp();
	~CMainApp();

	virtual void OnUpdate(real timeStep);
	virtual void OnDraw();

	void Run();
private:
	IKernel*		kernel;
	IVarSystem*		varSys;
	ISystem*		sys;
	IUpdateSystem*	updateSys;
	IInputSystem*	inputSys;
	IRenderDriver*	renderDriver;

	CNode*			root;
	CNode*			child1;
};

CMainApp::CMainApp(): IUpdateable("CMainApp") {	
	renderDriver = NULL;
	kernel = IKernel::GetKernel();

	if (!kernel->Init() ) {
		kernel->Shutdown();
		return;
	}

	varSys		= kernel->GetVarSystem();
	sys			= kernel->GetSystem();
	updateSys	= kernel->GetUpdateSystem();
	inputSys	= kernel->GetInputSystem();
	renderDriver= kernel->GetRenderDriver();

	updateSys->AddUpdater(this);
	updateSys->Print();

	renderDriver->ShowCursor(true);

	Log("Using kernel: %s\n", kernel->GetName().c_str());
	Log("OS: %s\n", sys->GetOS().c_str());
	Log("User data dir: %s\n", sys->GetUserDataDir().c_str());
	Log("ExecutableFilename: %s\n", sys->GetExecutableFilename().c_str());

	inputSys->AddAction(new CActionKeyboard("Escape", inputSys, key_ESCAPE));

	root	= new CNode("root");
	child1	= new CNode("child1");

	child1->Attach(root);
	root->PrintHierarchy();
}

CMainApp::~CMainApp(){
	SafeDelete(root);
	SafeDelete(child1);

	kernel->Shutdown();
}

void CMainApp::OnUpdate(real timeStep) {

	if (inputSys->CheckAction("Escape")) {
		kernel->Quit();
	}

}

void CMainApp::OnDraw() {
}

void CMainApp::Run() {
	kernel->Run();
}


int _tmain(int argc, _TCHAR* argv[]) {
	CMainApp* app = new CMainApp;

	app->Run();

	SafeDelete(app);

	return 0;
}

